package com.atguigu.stack;


//使用链表实现栈
public class StackLink {
    public static void main(String[] args) {
        LinkBoy link = new LinkBoy(5);
        Boy boy1 = new Boy(10);
        Boy boy2 = new Boy(20);
        Boy boy3 = new Boy(30);
        link.push(boy1);
        link.push(boy2);
        link.push(boy3);
        link.pop();
        link.pop();
        link.pop();
        link.list();
    }


}

class LinkBoy{
    private Boy head = new Boy(0);
    private int max;

    public LinkBoy(int max){
        this.max = max;
    }

    //判断列表是否为空
    public boolean isEmpty(){
        return head.next == null;
    }

    //判断列表是否已满
    public boolean ifFull(){
        int num = 0;
        Boy temp = head;
        if (!isEmpty()){
            while (true){
                if (temp==null){
                    break;
                }
                num++;
                temp = temp.next;
            }
        }
        return num == max;
    }

    //往列表里添加数据
    public void push(Boy val){
        Boy temp = head;
        if (ifFull()){
            System.out.println("stack已满，无法添加数据");
        }else {

            while (true){
                if (temp.next == null){
                    break;
                }
                temp = temp.next;
            }
            temp.next = val;
        }
    }

    //从列表里面取出数据
    public void pop(){
        Boy temp = head;
        if (temp.next == null){
            System.out.println("stack为空，无法取出数据");
        }
        while (true){
            if (temp.next != null){
                if (temp.next.next==null){
                    temp.next = null;
                    break;
                }
            }
            temp = temp.next;
        }
    }


    //遍历stack数据
    public void list(){
        if (isEmpty()){
            System.out.println("stack为空无法遍历数据");
        }

        Boy temp = head.next;
        while (true){
            if (temp == null){
                break;
            }
            System.out.println(temp.num);
            temp = temp.next;
        }
    }



}

class Boy{
    int num;
    Boy next;

    public Boy(int num){
        this.num = num;
    }
}